home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.4 / src / a_sort.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-11  |  6.4 KB  |  233 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: a_sort.c,v 5.4 1993/04/12 01:10:15 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 5.4 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1988-1992 USENET Community Trust
  8.  *             Copyright (c) 1986,1987 Dave Taylor
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log: a_sort.c,v $
  17.  * Revision 5.4  1993/04/12  01:10:15  syd
  18.  * fix @aliasname sort problem
  19.  * From: "Robert L. Howard" <robert.howard@matd.gatech.edu>
  20.  *
  21.  * Revision 5.3  1993/01/19  04:52:19  syd
  22.  *     add c)hange alias command to alias helpfile
  23.  *     if a deleted alias is changed, undelete it.  Also added the 'N'
  24.  * flag to changed aliases to help remind the user.  Documented it.
  25.  * Note:  if they mark the alias for deletion AFTER making the change it
  26.  * WILL be deleted. (and marked accordingly)
  27.  *     modified alias mode title string to indicate when a resync was
  28.  * needed.
  29.  *     allow editing alias file when none exist.
  30.  *     Now aliases are check for illegal characters (and WS) and
  31.  * addresses are check for illegal WS when they are being entered.  If
  32.  * anything illegal is found and message is printed and they keep entering
  33.  * the item until they get it right.
  34.  *     I fixed a couple of places where int should be long to match
  35.  * the declared type of alias_rec.length
  36.  * From: "Robert L. Howard" <robert.howard@matd.gatech.edu>
  37.  *
  38.  * Revision 5.2  1992/11/22  00:05:07  syd
  39.  * Fix bug where alias records were not sorting by both last and
  40.  * first names.
  41.  * From: "Robert L. Howard" <robert.howard@matd.gatech.edu>
  42.  *
  43.  * Revision 5.1  1992/10/03  22:58:40  syd
  44.  * Initial checkin as of 2.4 Release at PL0
  45.  *
  46.  *
  47.  ******************************************************************************/
  48.  
  49. /** Sort alias table by the field specified in the global
  50.     variable "alias_sortby"...
  51.  
  52. **/
  53.  
  54. #include "headers.h"
  55. #include "s_aliases.h"
  56.  
  57. char *alias_sort_name();
  58.  
  59. sort_aliases(entries, visible, are_in_aliases)
  60. int entries, visible, are_in_aliases;
  61. {
  62.     /** Sort the header_table definitions... If 'visible', then
  63.         put the status lines etc **/
  64.     
  65.     long last_index = -1;
  66.     int compare_aliases();    /* for sorting */
  67.  
  68.     dprint(2, (debugfile, "\n** sorting aliases by %s **\n\n", 
  69.         alias_sort_name(FULL)));
  70.  
  71.     /* Don't get last_index if no entries or no current. */
  72.     /* There would be no current if we are sorting for the first time. */
  73.     if (entries > 0 && current > 0)
  74.       last_index = aliases[current-1]->length;
  75.  
  76.     if ((entries > 30) && visible && are_in_aliases) {
  77.         error1(catgets(elm_msg_cat, AliasesSet, AliasesSort,
  78.             "Sorting messages by %s..."), alias_sort_name(FULL));
  79.     }
  80.     
  81.     if (entries > 1)
  82.       qsort((char *) aliases, (unsigned) entries,
  83.             sizeof (struct alias_rec *), compare_aliases);
  84.  
  85.     if (last_index > -1)
  86.       alias_old_current(last_index);
  87.  
  88.     if (are_in_aliases) {
  89.         clear_error();
  90.     }
  91. }
  92.  
  93. int
  94. compare_aliases(p1, p2)
  95. struct alias_rec **p1, **p2;
  96. {
  97.     /** compare two aliases according to the sortby value.
  98.  
  99.         Both are simple strcmp()'s on the alias or last_name
  100.         components of the alias.
  101.      **/
  102.  
  103.     register struct alias_rec *first, *second;
  104.     register int ret;
  105.     register long diff;
  106.  
  107.     first = *p1;
  108.     second = *p2;
  109.  
  110.     /* If (only) one of the compares is a duplicate we want it
  111.      * to go to the end of the list regardless of the sorting
  112.      * method.
  113.      */
  114.     if ((first->type ^ second->type) & DUPLICATE) {
  115.         if (first->type & DUPLICATE)
  116.             ret = 1;
  117.         else            /* It must be second... */
  118.             ret = -1;
  119.         return ret;
  120.     }
  121.  
  122.     switch (abs(alias_sortby)) {
  123.     case ALIAS_SORT:
  124.         ret = strcmp(first->alias, second->alias);
  125.         break;
  126.  
  127.     case NAME_SORT:
  128.         ret = strcmp(first->last_name, second->last_name);
  129.          /*
  130.           * If equal on last name then compare on first name
  131.           * which is the first part of 'name'.
  132.           */
  133.         if (ret == 0) {
  134.             ret = strcmp(first->name, second->name);
  135.         }
  136.         break;
  137.  
  138.     case TEXT_SORT:
  139.         diff = (first->length - second->length);
  140.          if ( diff < 0 )    ret = -1;
  141.          else if ( diff > 0 ) ret = 1;
  142.          else ret = 0;
  143.         break;
  144.  
  145.     default:
  146.         /* never get this! */
  147.         ret = 0;
  148.         break;
  149.     }
  150.  
  151.     if (alias_sortby < 0)
  152.       ret = -ret;
  153.  
  154.     return ret;
  155. }
  156.  
  157. char *alias_sort_name(type)
  158. int type;
  159. {
  160.     /** return the name of the current sort option...
  161.         type can be "FULL", "SHORT" or "PAD"
  162.     **/
  163.     int pad, abr;
  164.     extern char *a_rev_alias_pad, *a_rev_alias_abr, *a_rev_alias_name,
  165.             *a_rev_full_pad, *a_full_abr, *a_rev_full_name,
  166.             *a_rev_text_pad, *a_text_abr, *a_rev_text_file,
  167.             *a_alias_pad, *a_alias_abr, *a_alias_name,
  168.             *a_full_pad, *a_full_abr, *a_full_name,
  169.             *a_text_pad, *a_text_abr, *a_text_file;
  170.     
  171.     pad = (type == PAD);
  172.     abr = (type == SHORT);
  173.  
  174.     if (alias_sortby < 0) {
  175.       switch (- alias_sortby) {
  176.         case ALIAS_SORT   : return(
  177.                       pad?     "Reverse Alias Name      " :
  178.                   abr?     "Reverse-Alias" :
  179.                        "Reverse Alias Name");
  180.         case NAME_SORT    : return(
  181.                       pad?     "Reverse Full (Real) Name" :
  182.                   abr?     "Reverse-Name" :
  183.                        "Reverse Full (Real) Name");
  184.         case TEXT_SORT    : return(
  185.                       pad?     "Reverse Text File       " :
  186.                   abr?     "Reverse-Text" :
  187.                        "Reverse Text File");
  188.       }
  189.     }
  190.     else {
  191.       switch (alias_sortby) {
  192.         case ALIAS_SORT   : return(
  193.                       pad?     "Alias Name              " :
  194.                   abr?     "Alias" :
  195.                        "Alias Name");
  196.         case NAME_SORT    : return(
  197.                       pad?     "Full (Real) Name        " :
  198.                   abr?     "Name" :
  199.                        "Full (Real) Name");
  200.         case TEXT_SORT    : return(
  201.                       pad?     "Text File               " :
  202.                   abr?     "Text" :
  203.                        "Text File");
  204.       }
  205.     }
  206.  
  207.     return("*UNKNOWN-SORT-PARAMETER*");
  208. }
  209.  
  210. alias_old_current(iindex)
  211. long iindex;
  212. {
  213.     /** Set current to the message that has "index" as it's 
  214.         index number.  This is to track the current message
  215.         when we resync... **/
  216.  
  217.     register int i;
  218.  
  219.     dprint(4, (debugfile, "alias-old-current(%d)\n", iindex));
  220.  
  221.     for (i = 0; i < message_count; i++)
  222.       if (aliases[i]->length == iindex) {
  223.         current = i+1;
  224.         dprint(4, (debugfile, "\tset current to %d!\n", current));
  225.         return;
  226.       }
  227.  
  228.     dprint(4, (debugfile,
  229.         "\tcouldn't find current index.  Current left as %d\n",
  230.         current));
  231.     return;        /* can't be found.  Leave it alone, then */
  232. }
  233.